package com.scalesoft.artsync.frontend;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import com.scalesoft.artsync.frontend.controller.ProgressDialogController;
import com.scalesoft.artsync.frontend.view.ProgressDialogView;

public class ProgressDialog implements Progressable {

	private ProgressDialogController controller;
	
	private ProgressDialogView view;
	
	private static ProgressDialog instance = null;
	
	public static ProgressDialog getInstance() {
		if (instance == null) {
			instance = new ProgressDialog();
		}
		return instance;
	}
	
	public ProgressDialog() {
		view = new ProgressDialogView();
		controller = new ProgressDialogController(view);
	}

	@Override
	public void setMaximum(int value) {
		controller.setMaximum(value);
	}

	@Override
	public void setMinimum(int value) {
		controller.setMinimum(value);
	}

	@Override
	public void setValue(int value) {
		controller.setValue(value);
	}

	@Override
	public void setMessage(String message) {
		controller.setMessage(message);
	}
	
	public void setVisible(boolean visible) {
		Dimension dim = view.getToolkit().getScreenSize();
		Rectangle abounds = view.getBounds();
		view.setLocation((dim.width - abounds.width) / 2,
						(dim.height - abounds.height) / 2);
		view.setVisible(visible);
		view.requestFocus();
	}
	
	public static void main(String[] arg) {
		try {
		ProgressDialog d = new ProgressDialog();
		d.setMinimum(0);
		d.setMaximum(100);
		d.setValue(0);
		d.setVisible(true);
		Thread.sleep(2000);
		d.setValue(50);
		Thread.sleep(1000);
		d.setValue(70);
		Thread.sleep(1000);
		d.setValue(100);
		d.setVisible(false);
		
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void hide() {
		setVisible(false);
	}

	@Override
	public void show() {
		setVisible(true);
	}

	public JButton getButtonCancel() {
		return view.getButtonCancel();
	}

	@Override
	public void setActionListener(ActionListener listener) {
		if (view.getButtonCancel().getActionListeners() != null && view.getButtonCancel().getActionListeners().length > 0 && view.getButtonCancel().getActionListeners()[0] != null)
			view.getButtonCancel().removeActionListener(view.getButtonCancel().getActionListeners()[0]);
		view.getButtonCancel().addActionListener(listener);
		
	}

}
